package com.jjh.web.controller;

import com.jjh.service.impl.DishServiceImpl;
import com.jjh.service.impl.OrderServiceImpl;
import com.jjh.winterframework.annatation.Component;
import com.jjh.pojo.Dishes;
import com.jjh.pojo.Shop;
import com.jjh.service.DishService;
import com.jjh.service.OrderService;
import com.jjh.util.PageBody;
import com.jjh.web.Menu;
import com.jjh.web.view.DishView;
import com.jjh.winterframework.annatation.Autowired;
import com.jjh.wrapper.QueryWrapper;

import java.util.List;
import java.util.Scanner;

/**
 * 菜品显示类
 */
@Component
public class DishController implements DishView {


    private static Scanner scanner = new Scanner(System.in);

    /**
     * 调用菜品的服务
     */
    @Autowired
    private DishServiceImpl dishService;

    /**
     * 调用订单服务
     */
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private OrderController orderController;
    /**
     * 展示莫一具体的商家
     */
    @Override
    public void showDishes(Shop shop) {
        System.out.println("<=================================欢迎来到 ### "+shop.getName()+" ### 店" +
                "================================>");
        System.out.println("======>店家简介:");
        System.out.println(shop.getDesception());
        System.out.println(" -> 输入对应 序号 点餐");
        System.out.println(" -> 输入 up/down 来切换页面");
        System.out.println(" -> 输入 0 返回上一级菜单\r\n");
        //先把第一页食物查出来
        System.out.println("   序 号     菜 品     销 量     介 绍     余 量     单 价\r\n");

        QueryWrapper wrapper = new QueryWrapper();

        wrapper.eq("shopId",String.valueOf(shop.getId()));

        PageBody pageBody = dishService.pageList(1, 10,wrapper);
        int total = pageBody.getTotal();
        int currntPage = pageBody.getCurrntPage();
        int pageSize = pageBody.getPageSize();
        int size = pageBody.getSize();
        List<Object> dishList = pageBody.getResults();
        for(int i =1;i<=dishList.size();i++){
            Dishes dishes = (Dishes) dishList.get(i - 1);
            System.out.println("   "+i+"   \t"+dishes.getDname()+"   \t"+dishes.getCount()+"   \t"
                    +dishes.getDetails()+"   \t"+dishes.getNum()+"   \t"+dishes.getPrice()+"\r\n");
        }
        System.out.println("当前第 "+currntPage+"/"+pageSize+" 页  共 "+total+" 条数据");
        boolean flag = true;
        while (flag){
            System.out.print(" -> 请输入值:");
            String code = scanner.nextLine();
            switch (code){
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                case "10":
                     orderController.orderOne((Dishes)dishList.get(Integer.parseInt(code)-1),
                            currntPage,size,wrapper,shop);
                    flag =false;
                    break;
                case "up":
                    --currntPage;
                    if(currntPage<1){
                        currntPage = 1;
                        System.out.println("===>当前已经是第一页!\r\n");
                        break;
                    } else if (currntPage>pageSize) {
                        System.out.println("===>当前已经是最后一页!\r\n");
                        currntPage = pageSize;
                        break;
                    }
                    changeDishPage(currntPage,size,wrapper);
                    break;
                case "down":
                    ++currntPage;
                    if(currntPage<1){
                        currntPage = 1;
                        System.out.println("===>当前已经是第一页!\r\n");
                        break;
                    } else if (currntPage>pageSize) {
                        System.out.println("===>当前已经是最后一页!\r\n");
                        currntPage = pageSize;
                        break;
                    }
                   changeDishPage(currntPage,size,wrapper);
                    break;
                case "0":
                    Menu.subMenu01();
                    flag =false;
                    break;
                default:
                    System.out.println("=====>出错了! 请重新输入:");
                    break;
            }
        }

    }



    /**
     * 跟换菜品页面
     * @param currntPage 当前页面
     * @param size 尺寸
     */
    @Override
    public void changeDishPage(int currntPage, int size,QueryWrapper wrapper) {

        PageBody pageBody = dishService.pageList(currntPage,size,wrapper);
        int total = pageBody.getTotal();
        int pageSize = pageBody.getPageSize();
        List<Object> dishList = pageBody.getResults();
        for(int i =1;i<=dishList.size();i++){
            Dishes dishes = (Dishes) dishList.get(i - 1);
            System.out.println("   "+i+"   \t"+dishes.getDname()+"   \t"+dishes.getCount()+"   \t"
                    +dishes.getDetails()+"   \t"+dishes.getNum()+"   \t"+dishes.getPrice()+"\r\n");
        }
        System.out.println("当前第 "+currntPage+"/"+pageSize+" 页  共 "+total+" 条数据");
    }

}
